package com.example.easypan.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.easypan.common.Result;
import com.example.easypan.pojo.UserInfo;
import com.example.easypan.service.impl.UserInfoServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 功能：
 * 作者：cyan
 * 日期：2025/1/29 下午2:12
 */
@RestController
public class UserController {
    @Resource
    private UserInfoServiceImpl userInfoService;

    @GetMapping("/list")
    public Result getList()
    {
        List<UserInfo> list = userInfoService.list();
        return Result.success(list);
    }

    @PostMapping("/login")
    public Result login(@RequestBody UserInfo user)
    {
        if(StrUtil.isBlank(user.getUserId())||StrUtil.isBlank(user.getPassword()))
        {
            return Result.error("输入的数据为空");
        }

        UserInfo userRef=userInfoService.login(user);

        return Result.success(userRef);
    }

    //头像更新
    @PostMapping("/updateAvatar")
    public Result update(@RequestBody UserInfo user)
    {
        LambdaQueryWrapper<UserInfo> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(UserInfo::getUserId,user.getUserId());
        UserInfo dbuser=userInfoService.getOne(wrapper);

        dbuser.setAvatar(user.getAvatar());

        boolean update = userInfoService.update(dbuser, wrapper);

        return Result.success(update);
    }

    //详细信息更新
    @PostMapping("/updateDetail")
    public Result updateDetail(@RequestBody UserInfo user)
    {
        LambdaQueryWrapper<UserInfo> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(UserInfo::getUserId,user.getUserId());
        UserInfo dbuser=userInfoService.getOne(wrapper);

        dbuser.setNickName(user.getNickName());
        dbuser.setEmail(user.getEmail());
        dbuser.setPassword(user.getPassword());

        boolean update = userInfoService.update(dbuser, wrapper);

        return Result.success(update);
    }




}